#ifndef URLSNIFFER_H
#define URLSNIFFER_H

#include <QObject>
#include <QStringList>
#include <QPair>
#include <QList>
#include <QStringList>
#include <map>

class YDSniffer;

// 外部使用的 sniffer
class YDUrlSniffer : public QObject {
    Q_OBJECT
public:
    typedef std::map<QString, QStringList> job_t;
    // 如果 pair 的第二项是空字符串，表示获取失败
    typedef std::map<QString, QList<QPair<QString, QString>>> result_t;

    explicit YDUrlSniffer(QObject* parent = nullptr) : QObject(parent) {}
    ~YDUrlSniffer() {}

    void sniff(const job_t& job);

    int progress() const { // 0 - 100
        if (0 == this->count) {
            return 100;
        } else {
            return ((this->successCount + this->errorCount) * 100 / this->count);
        }
    }

    bool isFinished() const {
        return (this->progress() == 100);
    }

    result_t& results() {
        return this->resultsMap;
    }

signals:
    void progressChanged(int pgr);
    void finished();

private slots:
    void on_sniffer_getUrl(const QString& url, const QString& realUrl);
    void on_sniffer_error(const QString& url);

private:
    int successCount = 0;
    int errorCount = 0;
    int count = 0;
    result_t resultsMap;
};

#endif // URLSNIFFER_H
